export const throttle = (callback: (...args: any) => void, delay: number = 200, immediate: boolean = true) => {
    let preDate = 0;
    return (...args: any) => {
        if (immediate) {
            if (Date.now() - preDate > delay) {
                callback(...args);
                preDate = Date.now();
            }
        } else {
            if (preDate === 0) {
                let preDate = Date.now();
            } else {
                if (Date.now() - preDate > delay) {
                    callback(...args);
                }
            }
        }
    };
};
//异步轮询
export const polling = (conditionFunction: () => any, callback?: () => any, timeoutCb?: () => any, gap = 100, timeout = 30 * 1000) => {
    return new Promise((resolve, reject) => {
        let initialDate = Date.now();
        let nowDate;
        let timer = setInterval(() => {
            nowDate = Date.now();
            if (conditionFunction()) {
                clearInterval(timer);
                callback && callback();
                return resolve(true);
            }
            if (nowDate - initialDate >= timeout) {
                clearInterval(timer);
                if (timeoutCb) {
                    timeoutCb();
                    return reject();
                }
            }
        }, gap);
    });
};
